#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author：albert time:2019/9/11
# 调用百度ai
from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '17229182' #17229182
API_KEY = 'GyZcXPBDO8hQ8E2mFzMxZSYd' #GyZcXPBDO8hQ8E2mFzMxZSYd
SECRET_KEY = 'WBHKnjTG2vbVAdTtvinSXm193wMGStE1' #WBHKnjTG2vbVAdTtvinSXm193wMGStE1
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 语音识别
def The_text():
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    # 识别本地文件
    a = client.asr(get_file_content('en_outfile.wav'), 'wav', 16000, {
        'dev_pid': 1536,
    })
    return a
# 语音合成
def  Speech(txt):
    result = client.synthesis(txt, 'zh', 1, {
        'vol': 5,
    })
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('speech.mp3', 'wb') as f:
            f.write(result)

# if __name__ == '__main__':
#     txt = "09月 12日 08时 周四  阴转中雨  25-19°C"
#     print(type(txt))
#     print('正在输出语音.....')
#     Speech(txt)